package com.bath.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bath.Constants;
import com.bath.core.EntityManager;
import com.bath.model.HandStatus;
import com.bath.model.HandToken;
import com.bath.model.User;
@Service
@Transactional
public class HandTokenService extends EntityManager <HandToken, Long> {
	/**
	 * 根据ID查找到对应的手牌
	 * @param id
	 * @return
	 */
	public HandToken getById(Long id){
		
		HandToken obj = getUnique("id",id);
		return obj;
	}
	/**
	 * 根据手牌编号查找到对应的手牌
	 * @param strNum
	 * @return
	 */
	public HandToken getByNum(String strNum){
		
		HandToken obj = getUnique("handTokenNum",strNum);
		return obj;
	}
	/**
	 * 用来获得HandToken的信息列表
	 * @param validFlag 表示是否有效，0表示无效；1表示有效
	 * @param status 表示状态，1表示空闲；2表示使用，3表示预订
	 * @param typeFlag 0表示女，1表示男，2表示豪华，3表示普通
	 * @return
	 */
	public List<HandToken> getList(short typeFlag,Long handStatus_ID){
		
		return find("from HandToken  h where h.validFlag = 1 and h.typeFlag = ? and h.status.id = ?",typeFlag,handStatus_ID);
	}
	
	public List<HandToken> getTokenAll(short typeFlag){
		
		return find("from HandToken  h where h.validFlag = 1 and h.typeFlag = ? ",typeFlag);
	}
	
	public Object[] addHandToken(HandToken obj){
		
		Object[] rtn = new Object[]{"false", "", null};
		HandToken objHandToken = this.getByNum(obj.getHandTokenNum());
		if(objHandToken!=null)
		{
			save(obj);
			rtn[0] = "true";
			rtn[2] = obj;
		}
		return rtn;
	}
	
	public Object[] changeStatus(Long handTokenId,Long statusId){
		Object[] rtn = new Object[]{"false", "", null};
		HandToken obj = getById(handTokenId);
	
		merge(obj);
		return rtn;
	}
	
	/**
	 *��ȡ�����ƻ�Ů���ƿɹ����������ƺ���
	 * 
	 * @param typeFlag
	 * @return List
	 */
	public List<HandToken> billingHandToken(short typeFlag){
		return this.find("from HandToken h where h.validFlag=1 and h.typeFlag= ? and (h.status.id=? or h.status.id=?)", typeFlag, Constants.HAND_STATUS_FREE, Constants.HAND_STATUS_ORDER);
	}
	
	/**
	 *��ȡ�����ƻ�Ů���ƿɹ����������ƺ���
	 * 
	 * @param typeFlag
	 * @return List
	 */
	public List<HandToken> closeHandToken(short typeFlag){
		return this.find("from HandToken h where h.validFlag=1 and h.typeFlag= ? and h.status.id=? ", typeFlag, Constants.HAND_STATUS_USE);
	}
	
	/**
	 *������ƺ����ȡΨһ������
	 * 
	 * @param handTokenNum
	 * @return HandToken
	 */
	public HandToken  isExsit(String handTokenNum) {
		return this.getUnique(new String[] {"validFlag","handTokenNum"}, new Object[]{(short)1,handTokenNum});
	}

}
